using System;
using System.Collections.Generic;
using System.Text;
using SlimDX;
using Barrage.Global;
using SlimDX.Direct3D9;

namespace Barrage
{
    /// <summary>
    /// Material used to get and set mesh parameters for rendering
    /// </summary>
    public class Material
    {
        /// <summary>
        /// Ambient material component
        /// </summary>
        protected Color4 ambient = new Color4(1,0,0,0);
        /// <summary>
        /// Diffuse material component
        /// </summary>
        protected Color4 diffuse = new Color4(1,1,1,1);
        /// <summary>
        /// Specular material component
        /// </summary>
        protected Color4 specular = new Color4(1,1,1,1);

        /// <summary>
        /// Specular Shininess material component
        /// </summary>
        protected float shininess = 4;

        /// <summary>
        /// Ambient material component
        /// </summary>
        public Color4 Ambient { get { return ambient; } set { ambient = value; } }
        /// <summary>
        /// Diffuse material component
        /// </summary>
        public Color4 Diffuse { get { return diffuse; } set { diffuse = value; } }
        /// <summary>
        /// Specular material component
        /// </summary>
        public Color4 Specular { get { return specular; } set { specular = value; } }
        /// <summary>
        /// Specular Shininess material component
        /// </summary>
        public float Shininess { get { return shininess; } set { shininess = value; } }

        /// <summary>
        /// Shader float parameters
        /// </summary>
        protected List<float> floatParameters = new List<float>(1);
        /// <summary>
        /// Shader float parameters
        /// </summary>
        public List<float> FloatParameters { get { return floatParameters; } }

        /// <summary>
        /// Shader Vector4 parameters
        /// </summary>
        protected List<Vector4> vector4Parameters = new List<Vector4>(1);
        /// <summary>
        /// Shader Vector4 parameters
        /// </summary>
        public List<Vector4> Vector4Parameters { get { return vector4Parameters; } }
        
        /// <summary>
        /// Shader Matrix parameters
        /// </summary>
        protected List<Matrix> matrixParameters = new List<Matrix>(1);
        /// <summary>
        /// Shader Matrix parameters
        /// </summary>
        public List<Matrix> MatrixParameters { get { return matrixParameters; } }

        /// <summary>
        /// Shader Vector4 array parameters
        /// </summary>
        protected List<List<Vector4>> vector4ArrayParameters = new List<List<Vector4>>(1);
        /// <summary>
        /// Shader Vector4 array parameters
        /// </summary>
        public List<List<Vector4>> Vector4ArrayParameters { get { return vector4ArrayParameters; } }

        /// <summary>
        /// Technique used in the shader (null to use default technique)
        /// </summary>
        protected string technique = null;
        /// <summary>
        /// Technique used in the shader (null to use default technique)
        /// </summary>
        public string Technique { get { return technique; } set { technique = value; } }

        /// <summary>
        /// Shader textures
        /// </summary>
        protected List<BaseTexture> textures = new List<BaseTexture>(1);
        /// <summary>
        /// Shader textures
        /// </summary>
        public List<BaseTexture> Textures { get { return textures; } }

        /// <summary>
        /// First texture of the material texture list
        /// </summary>
        public BaseTexture Texture { get { if (textures.Count > 0) return textures[0]; return null; } set { if (textures.Count == 0) textures.Add(value); else textures[0] = value; } }

        /// <summary>
        /// Default constructor, creates an empty material with default colors
        /// </summary>
        public Material()
        {
        }

        /// <summary>
        /// Creates a material from a D3DX Material, getting its texture and material colors
        /// </summary>
        /// <param name="m"></param>
        public Material(ExtendedMaterial m)
        {
            ambient = m.MaterialD3D.Emissive;
            diffuse = m.MaterialD3D.Diffuse;
            specular = m.MaterialD3D.Specular;
            shininess = m.MaterialD3D.Power;

            if ((m.TextureFileName!=null)&&(m.TextureFileName.Length>0))
                try
                {
                    textures.Add(Barrage.Textures.TextureManager.Textures[m.TextureFileName]);
                }
                catch(Exception)
                {
                }
           
        }
    }
}
